function getStreamFromEvent(event)
{
	return Stream.streams[$(event.target).parent().parent().find('.name').text()];
}

function changeMode(event)
{
	var stream = getStreamFromEvent(event);
	
	stream.switchMode();
	
	$(event.target).text(stream.getMode());
	
	event.stopPropagation();
}

function changeView(event)
{
	var stream = getStreamFromEvent(event),
		view = parseInt($(event.target).text(), 10);
	
	stream.setView(view);
	
	$(event.target).parent().html(viewSelector(view));
	
	event.stopPropagation();
}

function viewSelector(view)
{
	var viewSelector = '';
	
	jQuery.each(
		View.views, function (i)
		{
			viewSelector +=
				'<span onclick="changeView(event)" class="clickable'
					+ (i === view ? ' selected' : '')
					+ '">' + i
				+ '</span>';
		});
	
	return viewSelector;
}

function selectStreams()
{
	// Check if all streams are selected or if none are selected
	var someVisible = false, someHidden = false;
	
	jQuery.each(
		Stream.streams, function ()
		{
			if (this.hidden) {
				someHidden = true;
			} else {
				someVisible = true;
			}
		});
	
	$('#hideAllButton, #showAllButton').removeClass('selected');
	
	if (!someVisible) {
		// All are hidden
		$('#hideAllButton').addClass('selected');
	} else if (!someHidden) {
		// All are visible
		$('#showAllButton').addClass('selected');
	}
}

$(function ()
{
	var streams = [];
	
	jQuery.each(
		Stream.streams, function ()
		{
			streams.push({
				Visible:
					'<input type="checkbox" class="visible"'
						+ (this.hidden ? '' : ' checked') + '>',
				Name:       '<span class="name">' + this.name + '</span>',
				'NumEvents': this.data.length,
				Color:      '<div class="colorSwatch"><input value="' + this.getColor() + '" size="15"></div>',
				Mode:
					'<span onclick="changeMode(event)" class="clickable">'
						+ this.getMode()
					+ '</span>',
				View:
					viewSelector(this.view)});
		});
	
	if (streams.length === 0) {
		streams.push({Visible: 'No streams yet !'});
	}
	
	$.table('#streamList').save(
		streams,
		{clear: true});
	
	$('#streamList')
		.tablesorter();
	
	// TODO : Use advanced content searching for tablesorter
	// TODO : Add tablesorter skin
	
	$('#streamList tr').click(function ()
	{
		var line = $(this),
		    name = line.find('.name').text(),
		    stream = Stream.streams[name];
		
		stream.setHidden(!stream.hidden);
		
		line.find('input.visible').attr('checked', !stream.hidden);
		
		line.toggleClass('selected2');
		
		if (stream.hidden) {
			line.css('background-color', 'transparent');
		} else {
			line.css('background-color', stream.getColor());
		}
		
		selectStreams();
	}).each(function ()
	{
		var line = $(this),
		    name = line.find('.name').text();
		
		if (!name) {
			return;
		}
		
		var stream = Stream.streams[name];
		
		line.addClass('clickable2').css('background-color', stream.getColor());
	});
	
	$('input.visible:checked').parent().parent()
		.addClass('selected2');
	
	$('.colorSwatch').addColorPicker({
		clickCallback: function (event) {
			var elem   = $(event.target),
				line   = elem.parent().parent().parent(),
				input  = elem.parent().find('input'),
				name   = $(event.target).parent().parent().parent().find('.name').text(),
				stream = Stream.streams[name],
				color  = elem.css('background-color');
			
			stream.setColor(color);
			
			if (!stream.hidden) {
				line.css('background-color', stream.getColor());
			}
			
			input.val(color);
			
			input.val();
		},
		colors: Color.list.slice(0, Math.min(Color.list.length, 25))
	}).click(function (event)
	{
		event.stopPropagation();
	});
	
	selectStreams();
});
